package com.sky.service.impl;

//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl  implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    public PageResult getDishPage(DishPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page = dishMapper.getDishPage(dto);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public DishVO getDishById(Long id) {

        // TODO  Dish dish = dishMapper.getDishById(id);   改为MP
        Dish dish = dishMapper.selectById(id);

        List<DishFlavor> flavors = dishFlavorMapper.getDishFlavorById(id);

        DishVO dishVO = new DishVO();

        BeanUtils.copyProperties(dish,dishVO);

        dishVO.setFlavors(flavors);

        return dishVO;

    }

    @Override
    @Transactional
    public void saveDishWithFlavor(DishDTO dto) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);

        dishMapper.saveDishWithFlavor(dish);

        //获取insert语句生成的主键值
        Long dishId = dish.getId();
        System.out.println("当前添加菜品的主键为："+dishId);
        List<DishFlavor> flavors = dto.getFlavors();

        for (DishFlavor f : flavors) {
            f.setDishId(dishId);
        }

        dishFlavorMapper.saveFlavors(flavors);

    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //1.起售的菜品不能被删除
        List<Dish> list = dishMapper.getDishByIds(ids);
        for (Dish dish : list) {
            if(StatusConstant.ENABLE == dish.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //TODO 2.被套餐关联的菜品不能删除，
        List<SetmealDish> setmeals = setmealDishMapper.getSetmealByDishIds(ids);
        if(setmeals.size() != 0){ //不能写为null，只能写长度！=0
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3.删除菜品后，关联的口味数据也要删除
        dishMapper.deleteByIds(ids);

        dishFlavorMapper.deleteByIds(ids);

    }

    @Transactional
    @Override
    public void updateDishWithFlavor(DishDTO dto) {

        //1.修改dish表中的数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.updateDish(dish);

        //2.删除dish_flavor表中的数据，然后重新新增
        //修改只是目标，不要被他禁锢住思维，实现方式千千万
        List<Long> list = new ArrayList<>();
        list.add(dto.getId());
        dishFlavorMapper.deleteByIds(list);

        //TODO 可以将dish.id都插入flavor中去
        if(dto.getFlavors().size() != 0){
            for(DishFlavor f : dto.getFlavors()){
                f.setDishId(dish.getId());
            }
            dishFlavorMapper.saveFlavors(dto.getFlavors());
        }
    }

    @Override
    @Transactional
    public void updateDishStatus(Integer status, Long id) {
        //1.切换当前菜品的状态
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.updateDish(dish);
        //TODO 2.如果是停售的话需要同步取消套餐的启售状态
        //TODO dish表，setmeal_dish表，setmeal表
        if(StatusConstant.DISABLE == status){
            //查询中间表中所有的setmeal键值即可，然后全部将其改为status = 0
            //1.根据dish_id查询setmeal_dish表中的所有关联的setmeal_id,利用集合进行接收
            List<Long> setmealIds = setmealDishMapper.getSetmealByDishId(id);
            if(null != setmealIds && setmealIds.size() != 0){
                //2.修改集合中id一样的setmeal表中的status状态,不好封装了直接传递修改人
                Long currentId = BaseContext.getCurrentId();
                setmealMapper.updateSetmealStatusByDish(setmealIds,currentId);
            }
        }
    }

    @Override
    public List<Dish> getDishByCategoryId(Integer categoryId) {
        return dishMapper.getDishByCategoryId(categoryId);
    }

    @Override
    public List<DishVO> getDishVOByCategoryId(Integer categoryId) {
        //1.获取Dish & category.name as categoryName
        List<DishVO> dishVOs = dishMapper.getDishVOByCategoryId(categoryId);
        //2.获取dish_flavor  dish.id = dish_flavor.dish_id
        for(DishVO d : dishVOs){
            d.setFlavors(dishFlavorMapper.getDishFlavorById(d.getId()));
        }
        //3.合成两个
        return dishVOs;
    }
}
